package edu.uj.petri.view;

import edu.uj.petri.commands.RemoveArcCommand;
import edu.uj.petri.commands.WeightDialogCommand;
import edu.uj.petri.document.Document;
import edu.uj.petri.model.NormalArc;
import java.awt.Color;
import java.awt.geom.Point2D;
import java.awt.geom.Rectangle2D;
import java.util.ResourceBundle;

/**
 * @author Pawel
 */
public class NormalArcView extends ArcView {
    private Label label;
    
    

    public NormalArcView(NodeView begin) {
        super(begin);
        arc = null;
    }

    public void draw(Canvas canvas) {
        
        if (begin == null) return;
        
        if (floatingEnd == null)
        {       
            Color color;
            if (selected) color = DiagramElement.SELECTED_COLOR;
            else color = DiagramElement.UNSELECTED_COLOR;
            
            for (ArcSegment s : segments)
                if (s.getNodeEnd() != null) {
                    s.drawArrow(canvas, color);
                    label.setPosition( new Point2D.Double((s.getX1() + s.getX2())/2, (s.getY1() + s.getY2())/2 ));
                }
                else s.draw(canvas);
            
        
           if (selected) for (LinkingPoint p: linkingPoints) p.draw( canvas );         
        }
        else 
        {
            ArcSegment.drawArrow( begin.position.getX(), begin.position.getY(), floatingEnd.getX(), floatingEnd.getY(), canvas, Color.BLACK, null);
        }
   
        if ((arc != null) && (((NormalArc)(arc)).getWeight() > 1)) 
            label.draw(canvas);
    }

    public ContextMenu getContextMenu() {
        ContextMenu retval = new ContextMenu();

        retval.addItem(ResourceBundle.getBundle("edu/uj/petri/messages").getString("Set_weight"),
                new WeightDialogCommand((NormalArc)(arc)));
        retval.addItem(ResourceBundle.getBundle("edu/uj/petri/messages").getString("Remove"),
                new RemoveArcCommand(this));
        return retval;
    }
    
    public void setEnd(NodeView end) {
        
        super.setEnd( end );
        label = new Label( new Point2D.Double((segments.get(0).getX1() + segments.get(0).getX2())/2,
                (segments.get(0).getY1() + segments.get(0).getY2())/2), "");
    }
    
    public void removeFrom(View view) {
     
        super.removeFrom( view );
        /*if (begin instanceof TransitionView) ((Transition)(begin.getNode())).addOutgoingArc(arc);
        if (end instanceof TransitionView) ((Transition)(end.getNode())).addIncomingArc(arc);*/
    }
    
    /**
     * handles weight change and reverse
     */
    public void notifyFromObserved(int eventCode) {
        
        if (eventCode == NormalArc.WEIGHT_CHANGE)
        {
            label.setText( Integer.toString(((NormalArc)(arc)).getWeight()) );
            observerList.notifyAllSubscribers(REDRAW);
        }
    }

    /**
     * returns false if such element cannot be end of arc (eg. method is called 
     *    with a place and the arc already begins in a place).
     */
    public boolean canBeEnd(NodeView node) {
        // TODO check begin and end
        return true;
    }

    public void reverse() {
        arc.reverse();
    }

    protected void finishCreating() {
        arc = Document.getActiveDocument().getModel().createNormalArc(begin.getNode(), end.getNode());
        arc.subscribe(this);        
    }

    public boolean crossesRectangle(Rectangle2D rect) {
        
        // TODO
        return false;
    }
    
    private static final long serialVersionUID = -7857962239369392685L;

}
